package Exercise;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Exercise3 {
    //扫描目标目录找到包含指定字符的文件
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查找的目标目录:");
        String destPath = sc.next();

        File destSource = new File(destPath);
        if(!destSource.exists()){
            System.out.println("输入的目录不存在!");
            return;
        }
        if(!destSource.isDirectory()){
            System.out.println("输入的不是有效的目录！");
            return;
        }
        System.out.println("请输入要查找的目标文件");
        String filePath = sc.next();
        List<File> list = new ArrayList<>();
        scannerSource(destSource, filePath, list);
        System.out.println("共找到" + list.size() + "条记录，分别是");
        for(File file : list){
            System.out.println(file.getAbsoluteFile());
        }
    }

    private static void scannerSource(File destSource, String filePath, List<File> list) {
        File[] files = destSource.listFiles();
        if(files == null || files.length == 0){
            return;
        }
        for(File file : files){
            if(file.isDirectory()){
                scannerSource(file, filePath, list);
            } else if(file.getName().contains(filePath)){
                list.add(file);
            }
        };
    }
}
